<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<title>title</title>
	<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../css/aui.css" />
	<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
	<link rel="stylesheet" type="text/css" href="../css/icons.css" />
	<script src="../script/jquery-1.9.1.min.js"></script>
	<script src="../script/bootstrap.min.js"></script>
	<style>
		.table tr:first-child {
			background: url(static/img/tableheader-bg.gif) no-repeat left top;
			background-color: #DCE9F2;
			border-top: 1px solid #CBD6DE;
			border-bottom: 1px solid #ADBECB;
			color: #467aa7;
			font-size: 13px;
			font-weight: bold;
		}

		.table {
			margin-bottom: 15px;
			width: 100%;
			border-collapse: collapse;
			font-size: 13px;
			margin-bottom: 15px;
			width: 100%;
			border-collapse: collapse;
		}

		.table td {
			height: 40px;
			text-align: center;
			border: 1px solid #CBD6DE;
			padding: 0;
			margin: 0;
			vertical-align: middle !important;
		}

		/*#chkHCTable{
				font-size: 10px;
		}*/
	</style>
</head>

<body>
	<header class="aui-bar aui-bar-nav main-header">
		<a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
			<span class="aui-iconfont aui-icon-left"></span>
		</a>
		<div class="aui-title" id="title">
			挂具保养(计划)
		</div>
	</header>
	<div class="aui-bar aui-bar-nav sub-header"></div>
	<div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
		<div class="white_background">
			<ul class="aui-list aui-form-list" style="font-size: 14px;">
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label" style="font-size: 14px;">维护任务</div>
						<input type="text" class="aui-input" placeholder="选择维护任务" style="width: 80%;font-size: 14px;"
							id="chkTaskCode" value="" onclick="goSelectChkTask()">
						<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectChkTask()"></i>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label" style="font-size: 14px;">执行状态</div>
						<div class="aui-list-item-input">
							<select id="zxState" style="font-size: 14px;">
								<option value="2">进行</option>
								<option value="3" selected="true">完成</option>
							</select>
						</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label" style="font-size: 14px;">执行结果</div>
						<div class="aui-list-item-input">
							<select id="zxResult" style="font-size: 14px;">
								<option value="0">未判定</option>
								<option value="1">正常</option>
								<option value="2">异常</option>
							</select>
						</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label" style="font-size: 14px;">备注</div>
						<div class="aui-list-item-input">
							<input type="text" class="aui-input" placeholder="请输入备注" style="width: 80%;font-size: 14px;"
								id="remarkValue">
						</div>
					</div>
				</li>
			</ul>

			<ul id="myTab" class="nav nav-tabs" style="background-color: #F4F4F4;font-size: 13px;">
				<li class="active" style="width: 100%;text-align:center;"> <a href="#item" data-toggle="tab"
						style="border:none;background-color: transparent;">检修项
					</a>

				</li>

			</ul>
			<div id="myTabContent" class="tab-content">
				<div class="tab-pane fade in active" id="item">
					<div style="background-color:white; height: 100%;width: 100%;">
						<table id="chkItemTable" class="table" style="width:200%;font-size: 0.6rem;">
							<thead id="chkItemTableHead">
							</thead>
							<tbody id="chkItemTableBody">
							</tbody>
							<tbody id="chkItemTableBehind">
							</tbody>
						</table>
						<div class="aui-content">
							<div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
								<p>
								</p>
								<div class="aui-btn aui-btn-info" onclick="doAdd()"
									style="width: 100%;height: 100%;display: none;">
									<span class="aui-iconfont" style="font-size: 20px;">添加检修项</span>

								</div>
								<p></p>
							</div>
							<div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
								<p>
								</p>
								<div class="aui-btn aui-btn-info" onclick="doDel()"
									style="width: 100%;height: 100%;display: none;">
									<span class="aui-iconfont" style="font-size: 20px;">删除检修项</span>

								</div>
								<p></p>
							</div>
							<div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
								<p>
								</p>
								<div class="aui-btn aui-btn-info" onclick="doSave()" id="submitButton"
									style="width: 100%;height: 100%;">
									<span class="aui-iconfont" style="font-size: 20px;">提交数据</span>

								</div>
								<p></p>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script>

	var taskId = "";
	var taskCode = "";
	var itemIds = "";
	var itemCodes = "";
	var itemNames = "";
	var itemValues = "";
	var sysidList = "";
	var josnList = [];
	var delItemIds = "";
	var str = "";
	var strHC = "";
	var strYQ = "";
	//检测维修耗材参数
	var HCId = "";
	var mrlGid = "";
	var mrlCode = "";
	var mrlName = "";
	var mrlExName = "";
	var qty = "";
	var measureName = "";
	var consumeType = "";
	var remark = "";
	var josnHCList = [];
	var delHCIds = "";

	//检测维修耗材仪器
	var YqId = "";
	var mrlYqGid = "";
	var mrlYqCode = "";
	var mrlYqName = "";
	var remarkYq = "";
	var josnYqList = [];
	var delYqIds = "";

	var YqIds = "";

	apiready = function () {
		// $("#rsCode").focus();
		// idSelector = "rsCode";
	}


	function toScanner() {
		var moduleScanner = api.require('moduleScanner');
		var params = {
			top: 150,//扫描窗口的上边距
			width: 250,//扫描窗口的宽
			height: 250,//扫描窗口的高
			useColor: false,//是否使用自己设定的颜色(startColor和endColor),
			//如果不使用设定的颜色就使用默认的那张网格图
			startColor: '#0F08C8FF',//渐变起始颜色
			endColor: '#7F08C8FF',//渐变结束颜色
			animTime: 2200,//扫描动画时间,单位为毫秒
		};
		moduleScanner.toScannerActivityForResult(params, function (ret, err) {
			var SMequipCode = ret;
			var text = document.getElementById("rsCode");
			text.value = SMequipCode;

		});

	}

	function goSelectChkTask() {
		var query = api.pageParam.query;
		var type = "";
		if (query.type != undefined && query.type != null && query.type != "") {
			type = query.type;
		}
		var url = "rsMntTaskSelectPlan.html";
		var title = "点检任务列表";
		api.openWin({
			name: 'about8',
			url: 'about8.html',
			pageParam: {
				url: url,
				title: title,
				//				SRequipCode: SRequipCode,
				type: type
			}
		});
	}

	function setTask(id, taskCode, planCode, chkGid, taskState, taskResult, remark) {
		console.log(id, taskCode, planCode, chkGid, taskState, taskResult, remark)
		taskId = id;
		$("#zxResult").val(taskResult);
		$("#chkTaskCode").attr("value", taskCode);
		$("#remarkValue").val(remark);
		if ("" != taskId) {
			//选择设备点检任务后，根据任务id获取点检项
			getEquipTaskItemByTask(id, chkGid);
		}
	}


	function getEquipTaskItemByTask(taskId, chkGid) {
		console.log(taskId)
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getChkItemByTaskPlan";
		var un = localStorage.getItem('un');

		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		params.returnAll = 'false';
		params.timeout = 60;

		var data = {
			values: {
				un: un,
				taskId: taskId,
				chkGid: chkGid
			}
		};


		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				//成功
				if (myobj.code == '0') {
					itemIds = "";
					itemCodes = "";
					itemNames = "";
					itemValues = "";
					sysidList = "";
					josnList = [];
					var list = myobj.list;
					var html = "";
					var html1 = "";
					html1 += "<tr id='tr1' color: #467aa7;>";
					html1 += "<td width='10%' algin='center'>序号</td>";
					html1 += "<td algin='center' style='display:none;'>编号</td>";
					html1 += "<td width='25%' algin='center'>点检项</td>";
					html1 += "<td width='25%' algin='center'>检测值</td>";
					html1 += "<td width='30%' algin='center'>备注</td></tr>";

					$api.html($api.byId("chkItemTableHead"), html1);
					str = "";
					console.log(JSON.stringify(list))
					if (list != undefined) {
						//遍历
						for (var i = 0; i < list.length; i++) {
							var sysid = list[i].sysid;
							var itemId = list[i].itemId;
							var itemCode = list[i].itemCode;
							var itemName = list[i].itemName;
							var chkMode = list[i].chkMode;
							var chkResult = list[i].chkResult;
							var Remark = list[i].Remark;

							if (i == 0) {
								itemIds = itemId;
								sysidList = sysid
								itemCodes = itemCode;
								itemNames = itemName;
							} else {
								itemIds = itemIds + "," + itemId;
								itemCodes = itemCodes + "," + itemCode;
								itemNames = itemNames + "," + itemName;
								sysidList = sysidList + "," + sysid
							}
							//定性
							if (chkMode == "0") {
								if (chkResult == "2") {
									str += "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
										+ itemId + "\");'></td><td algin='center' style='display:none;'>"
										+ itemCode + "</td><td width='25%' algin='center'>"
										+ itemName + "</td><td width='25%' algin='left'><select id='"
										+ itemId + "'><option value='1'>正常</option><option value='2' selected='true'>异常</option></select></td>"
								} else {
									str += "<tr ><td width='10%' algin='center'>" + "" + "<input type='checkbox' onclick='setDelItem(\""
										+ itemId + "\");'></td><td algin='center' style='display:none;'>"
										+ itemCode + "</td><td width='25%' algin='center'>"
										+ itemName + "</td><td width='25%' algin='left'><select id='"
										+ itemId + "'><option value='1' selected='true'>正常</option><option value='2'>异常</option></select></td>"
								}
								str += "<td width='300px' algin='center'><input id='" + itemId + 'x' + "' type='text' value='" + Remark + "'></td>"
									+ "</tr>";
							}
						}
					}
					$("#chkItemTableHead").append(str);
				} else if (myobj.code == '1') {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});
	}

	function setDelItem(id) {
		if (delItemIds == "") {
			delItemIds = id;
		} else {
			if (delItemIds.indexOf(id) > 0) {
				//已存在
				var delArr = delItemIds.split(",");
				delItemIds = "";
				for (i = 0; i < delArr.length; i++) {
					if (delArr[i] != id) {
						if (i == 0) {
							delItemIds = delArr[i];
						} else {
							delItemIds = delItemIds + "," + delArr[i];
						}
					}

				}
			} else {
				//不存在
				delItemIds = delItemIds + "," + id;
			}
		}
	}


	function doSave(type) {
		if (itemIds != "") {
			//点检项表格中有数据
			setItemValues();
			//提交数据到服务端
			doSubmit(type);
		} else {
			alert("无点检项信息，无法提交数据");
		}
	}

	function setItemValues() {
		var itemIdArr = itemIds.split(",");//检修项个数
		var itemValue = "";
		var un = localStorage.getItem('un');
		for (i = 0; i < itemIdArr.length; i++) {

			var taskState = document.getElementById("zxState").value;
			var taskResult = document.getElementById("zxResult").value;

			var itemCodeArr = itemCodes.split(",");
			var itemNameArr = itemNames.split(",");
			var sysidArr = sysidList.split(",")
			var checkResult = "";
			var remark = document.getElementById(itemIdArr[i] + "x").value;
			//获取下拉框选中的值
			itemValue = document.getElementById(itemIdArr[i]).value;//选中的值
			checkResult = itemValue;

			if (i == 0) {
				itemValues = itemValue;
			} else {
				itemValues = itemValues + "," + itemValue;
			}
			//将点检项的结果转化为jsonList
			var iObj = new itemObj(itemIdArr[i], itemCodeArr[i], itemNameArr[i], sysidArr[i], checkResult, un, taskState, taskResult, remark);
			josnList.push(iObj);
		}
	}


	function itemObj(chkItemId, chkItemCode, chkItemName, sysid, checkResult, chkPerson, taskState, taskResult, remark) {
		this.chkItemGid = chkItemId;
		this.chkItemCode = chkItemCode;
		this.chkItemName = chkItemName;
		this.sysid = sysid;
		this.checkResult = checkResult;
		this.taskState = taskState;
		this.taskResult = taskResult;
		this.remark = remark;
	}
	function doSubmit() {
		if (!clickThrottle()) {
			return;
		}
		const submitButton = document.getElementById('submitButton');

		submitButton.style.pointerEvents = "none"
		var type = document.getElementById("zxResult").value;
		var taskState = document.getElementById("zxState").value;
		var remarkValue = document.getElementById("remarkValue").value;

		console.log(taskId)
		api.showProgress();
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/saveTaskItemPlan";
		var un = localStorage.getItem('un');

		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		params.returnAll = 'false';
		params.timeout = 60;

		api.showProgress({
			title: '保存中...',
		});

		var data = {
			values: {
				un: un,
				taskId: taskId,
				rowData: JSON.stringify(josnList),
				type: type,
				taskState: taskState,
				remark: remarkValue
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				//成功
				if (myobj.code == '0') {

					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: "保存成功",
						duration: 3000
					});
					//清空所有信息，重新扫设备选任务
					taskId = "";
					taskCode = "";
					itemIds = "";
					itemCodes = "";
					itemNames = "";
					itemValues = "";
					sysidList = "";
					josnList = [];

					HCIds = "";
					mrlGids = "";
					mrlCodes = "";
					mrlNames = "";
					mrlExNames = "";
					qtys = "";
					measureNames = "";
					consumeTypes = "";
					josnHCList = [];

					YqIds = "";
					mrlYqGids = "";
					mrlYqCodes = "";
					mrlYqNames = "";
					remarkYqs = "";
					josnYqList = [];
					//清空设备扫码框				
					$("#rsCode").val("").focus();
					// 清空执行状态					
					$("#zxState").val("3");
					// 清空执行结果					
					$("#zxResult").val("");
					// 清空任务单备注				
					$("#remarkValue").val("");
					//清空任务选择
					$("#chkTaskCode").attr("value", "");
					//清空点检项表格
					$("#chkItemTableHead").children().remove();
					//$api.html($api.byId("chkItemTableHead"), "");
					// 清除检测维修耗材表格
					$("#chkHCTableHead").children().remove();
					//$api.html($api.byId("chkHCTableHead"), "");
					// 清除检测维修仪器表格
					$("#chkYqTableHead").children().remove();
					//$api.html($api.byId("chkYqTableHead"), "");
				} else if (myobj.code == '1') {
					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			} else {
				api.hideProgress();
				submitButton.style.pointerEvents = "auto"
				alert(JSON.stringify(err));
			}
		});
	}
	//返回按钮
	function closeWin() {
		api.closeWin({});
	}
</script>

</html>